home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’91 / DAL Files / DALtool 4⁄19 (System 6.x) / DalHandlers.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  6KB  |  327 lines

  1. #include "DalDemo.h"
  2.  
  3. extern EventRecord    gTheEvent;
  4. extern Boolean gDone;
  5. extern char dirty;
  6. extern Rect    gDragRect;
  7. extern MenuHandle    gAppleMenu, gFileMenu, gEditMenu;
  8. extern Boolean gDALActive, gDALMonitor;
  9. extern int gDALState;
  10.  
  11. /*** HandleIdle ***/
  12. HandleIdle()
  13. {
  14.     WindowPtr    aWind;
  15.     int            aType;
  16.     
  17.     aWind = FrontWindow();
  18.     aType = (int) WindowType(aWind);
  19.  
  20.     if (aType == DEMO_WINDOW)
  21.     {
  22.         TEIdle(((demoPeek)aWind)->curTE);
  23.         if (gDALActive) DALMonitorState((demoPeek)aWind);
  24.     }
  25. }
  26.  
  27. /*** HandleEvent ***/
  28. HandleEvent()
  29. {
  30.     char theChar;
  31.     Boolean inBackground;
  32.     
  33.     switch (gTheEvent.what)
  34.     {
  35.         case nullEvent:
  36.             HandleIdle();
  37.             break;
  38.         case mouseDown:
  39.             HandleMouseDown();
  40.             break;
  41.         case keyDown:
  42.         case autoKey:
  43.             theChar = gTheEvent.message & charCodeMask;
  44.             if ((gTheEvent.modifiers & cmdKey) != 0)
  45.             {
  46.                 AdjustMenus();
  47.                 HandleMenuChoice(MenuKey(theChar));
  48.             }
  49.             else
  50.                 HandleTEKey(theChar);
  51.             break;
  52.         case activateEvt:
  53.             HandleActivate((WindowPtr)gTheEvent.message, (gTheEvent.modifiers & activeFlag) != 0);
  54.             break;
  55.         case updateEvt:
  56.             HandleUpdate((WindowPtr)gTheEvent.message);
  57.             break;
  58.         case app4Evt:
  59.             if ((gTheEvent.message & SUSPEND_RESUME_BIT) == RESUMING)
  60.             {
  61.                 inBackground = (gTheEvent.message & 0x01) == 0;
  62.                 HandleActivate(FrontWindow(), !inBackground);
  63.             }
  64.             else
  65.                 HandleIdle();
  66.             break;
  67.     }
  68. }
  69.  
  70. /*** HandleUpdate ***/
  71. HandleUpdate(w)
  72. WindowPtr w;
  73. {
  74.     GrafPtr    savePort;
  75.         
  76.     GetPort( &savePort );
  77.     SetPort( w );
  78.     BeginUpdate( w );
  79.     EraseRect(&w->portRect);
  80.     DrawDemoWind(w);
  81.     EndUpdate( w );
  82.     SetPort( savePort );
  83. }
  84.  
  85. /*** HandleMouseDown ***/
  86. HandleMouseDown()
  87. {
  88.     WindowPtr    whichWindow;
  89.     short int     thePart;
  90.     long int    menuChoice,windSize;
  91.     
  92.     thePart = FindWindow(gTheEvent.where, &whichWindow);
  93.     switch (thePart)
  94.     {
  95.         case inMenuBar:
  96.             AdjustMenus();
  97.             menuChoice = MenuSelect( gTheEvent.where);
  98.             HandleMenuChoice(menuChoice);
  99.             break;
  100.         case inSysWindow:
  101.             SystemClick(&gTheEvent,whichWindow);
  102.             break;
  103.         case inDrag:
  104.             DragWindow(whichWindow, gTheEvent.where, &gDragRect);
  105.             break;
  106.         case inGoAway:
  107.             if (TrackGoAway(whichWindow, gTheEvent.where))
  108.                 HandleCloseWindow((demoPeek)whichWindow);
  109.             break;
  110.         case inContent:
  111.             if (whichWindow != FrontWindow())
  112.                 SelectWindow(whichWindow);
  113.             else
  114.                 DoContent(whichWindow, &gTheEvent);
  115.             break;
  116.     }
  117. }
  118.  
  119. /*** AdjustMenus ***/
  120. AdjustMenus()
  121. {
  122.     WindowPtr    window;
  123.     int            wType;
  124.     int            offset;
  125.     TEHandle    te;
  126.     
  127.     window = FrontWindow();
  128.     wType = WindowType(window);
  129.     
  130.     if (window == NIL)
  131.     {
  132.         DisableItem(gFileMenu,CLOSE_ITEM);
  133.         CheckItem(gFileMenu, MONITOR_ITEM,FALSE);
  134.         DisableItem(gFileMenu,MONITOR_ITEM);
  135.         EnableItem(gFileMenu,NEW_ITEM);
  136.         
  137.         DisableItem(gEditMenu,UNDO_ITEM);
  138.         DisableItem(gEditMenu,CUT_ITEM);
  139.         DisableItem(gEditMenu,COPY_ITEM);
  140.         DisableItem(gEditMenu,PASTE_ITEM);
  141.         DisableItem(gEditMenu,CLEAR_ITEM);
  142.     }
  143.     else if (wType == DA_WINDOW)
  144.     {
  145.         DisableItem(gFileMenu,CLOSE_ITEM);
  146.  
  147.         EnableItem(gEditMenu,UNDO_ITEM);
  148.         EnableItem(gEditMenu,CUT_ITEM);
  149.         EnableItem(gEditMenu,COPY_ITEM);
  150.         EnableItem(gEditMenu,PASTE_ITEM);
  151.         EnableItem(gEditMenu,CLEAR_ITEM);
  152.     }
  153.     else if (wType == DEMO_WINDOW)
  154.     {
  155.         EnableItem( gFileMenu, CLOSE_ITEM);
  156.         EnableItem( gFileMenu, MONITOR_ITEM);
  157.         DisableItem( gFileMenu, NEW_ITEM);
  158.  
  159.         DisableItem(gEditMenu,UNDO_ITEM);
  160.         DisableItem(gEditMenu,CUT_ITEM);
  161.         DisableItem(gEditMenu,COPY_ITEM);
  162.         DisableItem(gEditMenu,PASTE_ITEM);
  163.         DisableItem(gEditMenu,CLEAR_ITEM);
  164.         
  165.         te = ((demoPeek)window)->curTE;
  166.         if ( (*te)->selStart < (*te)->selEnd)
  167.         {
  168.             EnableItem(gEditMenu,CUT_ITEM);
  169.             EnableItem(gEditMenu,COPY_ITEM);
  170.             EnableItem(gEditMenu,CLEAR_ITEM);
  171.         }
  172.         if ( GetScrap(NIL, 'TEXT', &offset) > 0)
  173.             EnableItem(gEditMenu,PASTE_ITEM);
  174.     }
  175. }
  176.  
  177. /*** HandleMenuChoice ***/
  178. HandleMenuChoice(menuChoice)
  179. long int menuChoice;
  180. {
  181.     int theMenu;
  182.     int theItem;
  183.     
  184.     if (menuChoice != 0)
  185.     {
  186.         theMenu = HiWord(menuChoice);
  187.         theItem = LoWord(menuChoice);
  188.         switch (theMenu)
  189.         {
  190.             case APPLE_MENU_ID:
  191.                 HandleAppleChoice(theItem);
  192.                 break;
  193.             case FILE_MENU_ID:
  194.                 HandleFileChoice(theItem);
  195.                 break;
  196.             case EDIT_MENU_ID:
  197.                 HandleEditChoice(theItem);
  198.                 break;
  199.         }
  200.         HiliteMenu(0);
  201.     }
  202. }
  203.  
  204. /*** HandleAppleChoice ***/
  205. HandleAppleChoice (theItem)
  206. int theItem;
  207. {
  208.     Str255    accName;
  209.     int        accNumber;
  210.     
  211.     switch (theItem)
  212.     {
  213.         case ABOUT_ITEM:
  214.             NoteAlert(ABOUT_ALERT, NIL);
  215.             break;
  216.         default:
  217.             GetItem(gAppleMenu, theItem, accName);
  218.             accNumber = OpenDeskAcc(accName);
  219.             break;
  220.     }
  221. }
  222.  
  223. /*** HandleFileChoice ***/
  224. HandleFileChoice(theItem)
  225. int theItem;
  226. {
  227.     WindowPtr whichWindow;
  228.     
  229.     switch ( theItem)
  230.     {
  231.         case NEW_ITEM:
  232.             CreateSession();
  233.             break;
  234.         case CLOSE_ITEM:
  235.             if ((whichWindow = FrontWindow()) != NIL)
  236.                 HandleCloseWindow((demoPeek)whichWindow);
  237.             gDALMonitor = FALSE;
  238.             break;
  239.         case MONITOR_ITEM:
  240.             gDALMonitor ^= TRUE;
  241.             CheckItem(gFileMenu, MONITOR_ITEM,gDALMonitor);
  242.             break;
  243.         case QUIT_ITEM:
  244.             if ((whichWindow = FrontWindow()) != NIL)
  245.                 HandleCloseWindow((demoPeek)whichWindow);
  246.             gDALMonitor = FALSE;
  247.             gDone = TRUE;
  248.             break;
  249.     }
  250. }
  251.  
  252. /*** HandleEditChoice ***/
  253. HandleEditChoice( theItem)
  254. int theItem;
  255. {
  256.     TEHandle    te;
  257.     WindowPtr    window;
  258.     int            wType, length, i;
  259.     CharsHandle    text;
  260.     Str255        tempStr;
  261.     demoPeek    demoWind;
  262.     
  263.     if (!SystemEdit (theItem - 1))
  264.     {
  265.         window = FrontWindow();
  266.         wType = WindowType(window);
  267.         
  268.         if (wType == DEMO_WINDOW)
  269.         {
  270.             demoWind = (demoPeek) window;
  271.             te = demoWind->curTE;
  272.             switch (theItem)
  273.             {
  274.                 case UNDO_ITEM:
  275.                     break;
  276.                 case CUT_ITEM:
  277.                     if (ZeroScrap() == noErr)
  278.                     {
  279.                         TECut(te);
  280.                         AdjustScrollBar(demoWind);
  281.                         if (TEToScrap() != noErr)
  282.                             ZeroScrap();
  283.                     }
  284.                     break;
  285.                 case COPY_ITEM:
  286.                     if (ZeroScrap() == noErr)
  287.                     {
  288.                         TECopy(te);
  289.                         if (TEToScrap() != noErr)
  290.                             ZeroScrap();
  291.                     }
  292.                     break;
  293.                 case PASTE_ITEM:
  294.                     if (TEFromScrap() == noErr)
  295.                     {
  296.                         TEPaste(te);
  297.                         AdjustScrollBar(demoWind);
  298.                     }
  299.                     break;
  300.                 case CLEAR_ITEM:
  301.                     TEDelete(te);
  302.                     AdjustScrollBar(demoWind);
  303.                     break;
  304.             } /* switch */
  305.         } /* if my wType */
  306.     } /* !SystemEdit */
  307. } /*HandleEditChoice*/
  308.  
  309. /*** ErrorHandler ***/
  310. ErrorHandler(strNum)
  311. int strNum;
  312. {
  313.     StringHandle    errStrH;
  314.     
  315.     if ((errStrH = GetString(strNum)) == NIL) 
  316.         ParamText(FATAL_STR,NIL,NIL,NIL);
  317.     else
  318.     {
  319.         HLock(errStrH);
  320.         ParamText(*errStrH,NIL,NIL,NIL);
  321.         HUnlock(errStrH);
  322.     }
  323.     StopAlert(ERROR_ALERT_ID,NIL);
  324.     ExitToShell();
  325. }
  326.  
  327.